package com.scm.erp.framework.controller;

import com.scm.erp.framework.pojo.systemConfiguration;
import com.scm.erp.framework.repository.netWorkImagesRepository;
import com.scm.erp.framework.repository.systemConfigurationRepository;
import com.scm.erp.framework.threadevent.getUrlImages;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Random;

/**
 *
 * 配置初始化默认控制器
 * */

@Controller
@CrossOrigin
@RequestMapping(value ="/")
public class loginController {
    @Resource
    private netWorkImagesRepository imagesRepository;
    @Resource
    private systemConfigurationRepository configurationRepository;
    @Resource
    private getUrlImages getImages;
    @Resource(name = "threadPoolConfiguration")
    ThreadPoolTaskExecutor asyncConfigurationThread;




    @RequestMapping(value ="/loginAction",produces = "text/html;charset=utf-8")
    public String loginPage(){
        return "login";
    }
    @RequestMapping(value ="/updateImageAction")
    public String updateImage(String isUpdate){
        if(isUpdate.equals("false") == true){
            System.out.println("=========== 关闭线程池");
            getImages.setStop(true);
            asyncConfigurationThread.destroy();
            asyncConfigurationThread.shutdown();
        }
        if (isUpdate.equals("true") == true){
            asyncConfigurationThread.execute(new Runnable() {
                @Override
                public void run() {
                    synchronized (this){
                       try {
                           getImages.setStop(false);
                           getImages.getUrlImages();
                           getImages.dbIsImgUrl();
                       }catch (NullPointerException e){
                           System.out.println("强制释放");
                       }
                    }
                }
            });
            asyncConfigurationThread.execute(new Runnable() {
                @Override
                public void run() {
                    synchronized (this){
                        try {
                            getImages.dbIsImgUrl();
                        }catch (NullPointerException e){
                            System.out.println("强制释放");
                        }
                    }
                }
            });
            asyncConfigurationThread.initialize();
        }
        return "login";
    }

    @ResponseBody
    @RequestMapping(value ="/refreshJson",produces = "text/html;charset=utf-8")
    public String refreshRebutActions(String open){
        Long id = imagesRepository.findMaxNId();
        if(open.equals("1")==true){
            return configurationRepository.findByIdAndSImagesUrl(1L);
        }
        if(open.equals("0")==true){
            Random random = new Random();
            String isImgUrl = imagesRepository.findByCuntNImagesUrl(Long.valueOf(random.nextInt(id.intValue())));
            systemConfiguration s = new systemConfiguration();
            s.setT_Sid(1L);
            s.setT_SImagesUrl(isImgUrl);
            configurationRepository.save(s);
            return configurationRepository.findByIdAndSImagesUrl(1L);
        }
        return "/";
    }
    @RequestMapping(value = "/registered",produces = "text/html;charset=utf-8")
    public String registeredAction(){
        return "registered";
    }
    @RequestMapping(value = "/ChangeAccount",produces = "text/html;charset=utf-8")
    public String ChangeAccountAction(){
        return "ChangeAccount";
    }
    @RequestMapping(value = "/admin",produces = "text/html;charset=utf-8")
    public String adminAction(){
        return "admin";
    }

}
